﻿using System;
using System.Data;
using BLL;
using DAL;
using Ext.Net;
using Roles;

namespace ERP.Modules
{
    public partial class Contract : System.Web.UI.UserControl
    {
        #region[Define]
        private static bool _flag;
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                CheckPermisstion();
                GetContract();
                GetTypeContract();
                GetJob();
                btnEdit.Disable(true);
                btnDel.Disable(true);
                btnExport.Disable(true);
            }
        }
        //Store
        protected void StoreContract_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearch.Text = string.Empty;
            GetContract();
        }
        protected void StoreContractEmployee_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearchContractEmployee.Text = string.Empty;
            GetEmployee();
        }
        //Button
        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            _flag = true;
            TabPanelMain.SetActiveTab(TabInfoCompany);
            ClearText();
            WindowsContract.Show();
        }
        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            var sm = grContract.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    _flag = false;
                    TabPanelMain.SetActiveTab(TabInfoCompany);
                    ClearText();
                    GetContractEdit(int.Parse(row.RecordID));
                    WindowsContract.Show();
                }
        }
        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            var sm = grContract.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    try
                    {
                        var obj = new EtContract { Contract_ID = int.Parse(row.RecordID) };
                        BllContract.Delete(obj);
                    }
                    catch (Exception ex)
                    {
                        HTML.clsFormat.Alert_ERROR("Thông Báo", ex.Message);
                    }
                }
            GetContract();
        }
        protected void btnSavePopUp_Click(object sender, DirectEventArgs e)
        {
            Insert();
        }
        protected void btnCancelPopUp_Click(object sender, DirectEventArgs e)
        {
            WindowsContract.Close();
        }
        protected void btnReloadCode_Click(object sender, DirectEventArgs e)
        {
            txtContract_Code.Text = AutoCode();
        }
        protected void btnEmployee_Click(object sender, DirectEventArgs e)
        {
            GetEmployee();
            WindowContractEmployee.Show();
        }
        //Double-Click
        protected void ContractDblClick(object sender, DirectEventArgs e)
        {
            _flag = false;
            TabPanelMain.SetActiveTab(TabInfoCompany);
            ClearText();
            int Contract_ID = int.Parse(e.ExtraParams["Contract_ID"]);
            GetContractEdit(Contract_ID);
            WindowsContract.Show();
        }
        //Cell-Click
        protected void CellContractEmployee_Click(object sender, DirectEventArgs e)
        {
            var sm = grContractEmployee.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                var id = sm.SelectedCell.RecordID;
                GetEmployeeEdit(int.Parse(id));
            }
            if (sm != null)
            {
                sm.Clear();
                sm.UpdateSelection();
            }
            WindowContractEmployee.Close();
        }
        //Search
        protected void ContractEmployee_Event(object sender, DirectEventArgs e)
        {
            var _clsEmployee = new BllEmployee();
            var _dtGetEmpoyee = _clsEmployee.GetEmployee(txtSearch.Text);
            if (_dtGetEmpoyee != null && _dtGetEmpoyee.Rows.Count > 0)
            {
                StoreContractEmployee.DataSource = _dtGetEmpoyee;
                StoreContractEmployee.DataBind();
            }
            else
            {
                grContractEmployee.GetStore().RemoveAll();
            }
        }
        protected void Search_Event(object sender, DirectEventArgs e)
        {
            var _clsContract = new BllContract();
            var _dtGetContract = _clsContract.GetContract(txtSearch.Text);
            if (_dtGetContract != null && _dtGetContract.Rows.Count > 0)
            {
                StoreContract.DataSource = _dtGetContract;
                StoreContract.DataBind();
            }
            else
            {
                grContract.GetStore().RemoveAll();
            }
        }
        #endregion

        #region[Method]
        private void GetJob()
        {
            var _clsGetJob = new Job_BLL();
            var _dtGetJob = _clsGetJob.BindGirdtb_Job(string.Empty);
            if (_dtGetJob != null && _dtGetJob.Rows.Count > 0)
            {
                StoreContractJob.DataSource = _dtGetJob;
                StoreContractJob.DataBind();
            }
            else
            {
                SelectBoxContractJob.GetStore().RemoveAll();
            }
        }
        private void GetTypeContract()
        {
            var _clsGetTypeContract = new BllTypeContract();
            var _dtGetTypeContract = _clsGetTypeContract.GetTypeContract(string.Empty);
            if (_dtGetTypeContract != null && _dtGetTypeContract.Rows.Count > 0)
            {
                StoreContractType.DataSource = _dtGetTypeContract;
                StoreContractType.DataBind();
            }
            else
            {
                SelectBoxTypeContract.GetStore().RemoveAll();
            }
        }
        private void GetEmployee()
        {
            var _clsEmployee = new BllEmployee();
            var _dtGetEmpoyee = _clsEmployee.GetEmployee(string.Empty);
            if (_dtGetEmpoyee != null && _dtGetEmpoyee.Rows.Count > 0)
            {
                StoreContractEmployee.DataSource = _dtGetEmpoyee;
                StoreContractEmployee.DataBind();
            }
            else
            {
                grContractEmployee.GetStore().RemoveAll();
            }
        }
        private void GetEmployeeEdit(int id)
        {
            var _clsEmployeeEdit = new BllEmployee();
            var _dtGetEmpoyeeEdit = _clsEmployeeEdit.GetEmployeeEdit(id);
            if (_dtGetEmpoyeeEdit != null && _dtGetEmpoyeeEdit.Rows.Count > 0)
            {
                txtEmployee_ID.Text = _dtGetEmpoyeeEdit.Rows[0]["Employee_Id"].ToString();
                txtFullName.Text = _dtGetEmpoyeeEdit.Rows[0]["FullName"].ToString();
                txtBrithday.Text = _dtGetEmpoyeeEdit.Rows[0]["Brithday"].ToString();
                txtJob_Name_Emp.Text = _dtGetEmpoyeeEdit.Rows[0]["Job_Name"].ToString();
                txtAddress_Emp.Text = _dtGetEmpoyeeEdit.Rows[0]["Address"].ToString();
                txtCMND.Text = _dtGetEmpoyeeEdit.Rows[0]["CMND"].ToString();
                txtEducationalLevel.Text = _dtGetEmpoyeeEdit.Rows[0]["EducationalLevel"].ToString();
            }
        }
        private void GetContract()
        {
            var _clsContract = new BllContract();
            var _dtGetContract = _clsContract.GetContract(string.Empty);
            if (_dtGetContract != null && _dtGetContract.Rows.Count > 0)
            {
                StoreContract.DataSource = _dtGetContract;
                StoreContract.DataBind();
            }
            else
            {
                grContract.GetStore().RemoveAll();
            }
        }
        private void GetContractEdit(int id)
        {
            var _clsContractEdit = new BllContract();
            var _dtGetContractEdit = _clsContractEdit.GetContractEdit(id);
            if (_dtGetContractEdit != null && _dtGetContractEdit.Rows.Count > 0)
            {
                lbID.Text = _dtGetContractEdit.Rows[0]["Contract_ID"].ToString();
                txtContract_Code.Text = _dtGetContractEdit.Rows[0]["Contract_Code"].ToString();
                SelectBoxTypeContract.SetValue(int.Parse(_dtGetContractEdit.Rows[0]["TypeContract_ID"].ToString()));
                txtDateBegin.Text = _dtGetContractEdit.Rows[0]["DateBegin"].ToString();
                txtContract_Name.Text = _dtGetContractEdit.Rows[0]["Contract_Name"].ToString();
                txtContract_Job.Text = _dtGetContractEdit.Rows[0]["Contract_Job"].ToString();
                txtContract_National.Text = _dtGetContractEdit.Rows[0]["Contract_National"].ToString();
                txtContract_Identification.Text = _dtGetContractEdit.Rows[0]["Contract_Identification"].ToString();
                txtContract_Company.Text = _dtGetContractEdit.Rows[0]["Contract_Company"].ToString();
                txtContract_Address.Text = _dtGetContractEdit.Rows[0]["Contract_Address"].ToString();
                txtContract_Phone.Text = _dtGetContractEdit.Rows[0]["Contract_Phone"].ToString();
                txtContract_Fax.Text = _dtGetContractEdit.Rows[0]["Contract_Fax"].ToString();

                txtFullName.Text = _dtGetContractEdit.Rows[0]["FullName"].ToString();
                txtEmployee_ID.Text = _dtGetContractEdit.Rows[0]["Employee_Id"].ToString();
                txtBrithday.Text = _dtGetContractEdit.Rows[0]["Brithday"].ToString();
                txtEducationalLevel.Text = _dtGetContractEdit.Rows[0]["EducationalLevel"].ToString();
                txtJob_Name_Emp.Text = _dtGetContractEdit.Rows[0]["Job_Name_Emp"].ToString();
                txtCMND.Text = _dtGetContractEdit.Rows[0]["CMND"].ToString();
                txtAddress_Emp.Text = _dtGetContractEdit.Rows[0]["Address_Emp"].ToString();

                txtContract_TimeWork.Text = _dtGetContractEdit.Rows[0]["Contract_TimeWork"].ToString();
                SelectBoxContractJob.SetValue(int.Parse(_dtGetContractEdit.Rows[0]["Job_ID"].ToString()));
                txtContract_Wage.Text = _dtGetContractEdit.Rows[0]["Contract_Wage"].ToString();
                txtContract_Vacation.Text = _dtGetContractEdit.Rows[0]["Contract_Vacation"].ToString();
                txtContract_Interest.Text = _dtGetContractEdit.Rows[0]["Contract_Interest"].ToString();
                txtContract_DateBegin.Text = _dtGetContractEdit.Rows[0]["Contract_DateBegin"].ToString();
                txtContract_DateEnd.Text = _dtGetContractEdit.Rows[0]["Contract_DateEnd"].ToString();
            }
        }
        private bool CheckInsert()
        {
            bool kiemtra = false;
            var _clsGetContract = new BllContract();
            var _dtTypeContract = _clsGetContract.GetContract(string.Empty);
            if (_dtTypeContract != null && _dtTypeContract.Rows.Count > 0)
            {
                foreach (DataRow dr in _dtTypeContract.Rows)
                {
                    if (lbID.Text != dr["Contract_ID"].ToString())
                    {
                        if (txtContract_Code.Text == dr["Contract_Code"].ToString())
                        {
                            kiemtra = true;
                            HTML.clsFormat.Alert_ERROR("Thông Báo", "Số hợp đồng này đã tồn tại !");
                        }
                    }
                }
            }
            return kiemtra;
        }
        private void Insert()
        {
            if (_flag)
            {
                InsertValue();
            }
            else
            {
                if (CheckPermisstionDobclick())
                {
                    HTML.clsFormat.Alert_ERROR("Thông Báo", "Bạn không được cấp quyền để sửa đổi !");
                }
                else
                {
                    UpdateValue();
                }
            }
        }
        private void InsertValue()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new EtContract
                    {
                        TypeContract_ID = int.Parse(SelectBoxTypeContract.SelectedItem.Value),
                        Job_ID  = int.Parse(SelectBoxContractJob.SelectedItem.Value),
                        Employee_Id = int.Parse(txtEmployee_ID.Text),
                        Contract_Code = txtContract_Code.Text,
                        DateBegin = DateTime.Parse(txtDateBegin.Text),
                        Contract_Name = txtContract_Name.Text,
                        Contract_Job = txtContract_Job.Text,
                        Contract_National = txtContract_National.Text,
                        Contract_Identification = txtContract_Identification.Text,
                        Contract_Company = txtContract_Company.Text,
                        Contract_Address = txtContract_Address.Text,
                        Contract_Phone = txtContract_Phone.Text,
                        Contract_Fax =txtContract_Fax.Text,
                        Contract_TimeWork =int.Parse(txtContract_TimeWork.Text),
                        Contract_Wage = float.Parse(txtContract_Wage.Text),
                        Contract_Vacation = float.Parse(txtContract_Vacation.Text),
                        Contract_Interest =txtContract_Interest.Text,
                        Contract_DateBegin = DateTime.Parse(txtDateBegin.Text),
                        Contract_DateEnd = DateTime.Parse(txtContract_DateEnd.Text),
                        Contract_File = DBNull.Value.ToString(),
                        Contract_Node = DBNull.Value.ToString(),
                    };
                    BllContract.Insert(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công !");
                    ClearText();
                    GetContract();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private void UpdateValue()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new EtContract
                    {
                        Contract_ID = int.Parse(lbID.Text),
                        TypeContract_ID = int.Parse(SelectBoxTypeContract.SelectedItem.Value),
                        Job_ID = int.Parse(SelectBoxContractJob.SelectedItem.Value),
                        Employee_Id = int.Parse(txtEmployee_ID.Text),
                        Contract_Code = txtContract_Code.Text,
                        DateBegin = DateTime.Parse(txtDateBegin.Text),
                        Contract_Name = txtContract_Name.Text,
                        Contract_Job = txtContract_Job.Text,
                        Contract_National = txtContract_National.Text,
                        Contract_Identification = txtContract_Identification.Text,
                        Contract_Company = txtContract_Company.Text,
                        Contract_Address = txtContract_Address.Text,
                        Contract_Phone = txtContract_Phone.Text,
                        Contract_Fax = txtContract_Fax.Text,
                        Contract_TimeWork = int.Parse(txtContract_TimeWork.Text),
                        Contract_Wage = float.Parse(txtContract_Wage.Text),
                        Contract_Vacation = float.Parse(txtContract_Vacation.Text),
                        Contract_Interest = txtContract_Interest.Text,
                        Contract_DateBegin = DateTime.Parse(txtDateBegin.Text),
                        Contract_DateEnd = DateTime.Parse(txtContract_DateEnd.Text),
                        Contract_File = DBNull.Value.ToString(),
                        Contract_Node = DBNull.Value.ToString(),
                    };
                    BllContract.Update(obj);
                    HTML.clsFormat.Alert_INFO("Thông Báo", "Cập nhật thành công !");
                    GetContract();
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        #endregion

        #region[ClearText]
        private void ClearText()
        {
            FormPanelContract.Reset();
            lbID.Text = string.Empty;
            txtContract_Code.Text = AutoCode();
            txtDateBegin.Text = DateTime.Now.ToString();
            txtContract_DateBegin.Text = DateTime.Now.ToString();
            txtContract_DateEnd.Text = DateTime.Now.ToString();
        }
        #endregion

        #region[AutoCode]
        private string AutoCode()
        {
            int i;
            var _clsGetContract = new BllContract();
            var dt = _clsGetContract.GetContract(string.Empty);
            if (dt != null && dt.Rows.Count > 0)
            {
                int max = int.Parse(dt.Rows[0]["Contract_ID"].ToString());
                for (i = 0; i < dt.Rows.Count; i++)
                {
                    if (int.Parse(dt.Rows[i]["Contract_ID"].ToString()) > max)
                    {
                        max = int.Parse(dt.Rows[i]["Contract_ID"].ToString());
                    }
                }
                i = max + 1;
            }
            else
            {
                i = 0;
            }
            return "HĐ-00" + i;
        }
        #endregion

        #region[Permisstion]
        private void CheckPermisstion()
        {
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.Contract, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnAdd == false)
                {
                    btnAddNew.Visible = false;
                }
                if (Is_BtnEdit == false)
                {
                    btnEdit.Visible = false;
                }
                if (Is_BtnView == false)
                {
                    PanelContract.Visible = false;
                }
                if (Is_BtnDel == false)
                {
                    btnDel.Visible = false;
                }
                if (Is_BtnPrint == false)
                {
                    btnExport.Visible = false;
                }
            }
        }
        private bool CheckPermisstionDobclick()
        {
            bool check = false;
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.Contract, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnEdit == false)
                {
                    check = true;
                }
            }
            return check;
        }
        #endregion
    }
}